package com.wanban.downloader.utils

import android.text.TextUtils
import com.blankj.utilcode.util.FileUtils
import com.wanban.downloader.MultiDownloadManager
import java.io.File
import java.lang.NullPointerException

object FileDownloadPathUtils {

    const val ROOT_FILE_DIR_NAME = "Wanban"
    const val LOCAL_M3U_FILE_DIR_NAME = "M3U"
    const val KEY_FILE_DIR_NAME = "Keys"
    const val PROXY_M3U_FILE_DIR_NAME = "Proxy"
    const val TS_LIST_FILE_DIR_NAME = "TsList"
    const val MP4_FILE_DIR_NAME = "Mp4"
    const val CACHE_TS_LIST_FILE_DIR_NAME = "CacheTsList"
    const val LOCAL_M3U8_FILE_DIR_NAME = "LocalTsList"


    const val LOCAL_M3U_FILE_NAME = "local.m3u"
    const val PROXY_M3U_FILE_NAME = "proxy.m3u8"
    const val MP4_FILE_NAME = "final.mp4"
    const val CACHE_TS_LIST_FILE_NAME = "cache_ts_list.txt"
    const val COVER_FILE_NAME = "cover.jpg"
    const val LOCAL_M3U8_FILE_NAME = "local.m3u8"

    const val LOCAL_CACHE_FILE_DIR_NAME = "Cache"
    const val LOCAL_CACHE_M3U8_FILE_NAME = "cacheM3U8.txt"

    /**
     * 创建并返回下载的根目录
     * /Data/Android/xxx/files/Wanban
     */
    fun getOrCreateDownloadRootFileDirPath() : String? {
        try {
            val extraFiles = ContextProvider.application.getExternalFilesDirs(null)
            if (extraFiles.isEmpty()) return null
            val rootPath = if (MultiDownloadManager.isDownloadSdcard()) { // sdcard
                if (extraFiles.size <= 1) { // 没插入sdcard
                    extraFiles[0].absolutePath
                } else {
                    extraFiles[1].absolutePath
                }
            } else {
                extraFiles[0].absolutePath
            }
            val downloadRootDirPath = rootPath + File.separator + ROOT_FILE_DIR_NAME
            val isCreateRootSuccess = FileUtils.createOrExistsDir(downloadRootDirPath)
            return if (isCreateRootSuccess) {
                downloadRootDirPath
            } else {
                null
            }
        } catch (e: NullPointerException) {
            return null
        } catch (e: Exception) {
            return null
        }

    }

    /**
     * 根据视频名称，创建当前视频下载的根目录
     * @param fileName
     *   /Data/Android/xxx/files/Wanban/${fileName}
     */
    fun getOrCreateVideoRootFileDirPath(fileName: String): String? {
        val root = getOrCreateDownloadRootFileDirPath()
        if (TextUtils.isEmpty(root)) return null
        val videoFileDirPath = root + File.separator + fileName
        val createCoverSuccess = FileUtils.createOrExistsDir(videoFileDirPath)
        return if (createCoverSuccess)
            videoFileDirPath
        else
            null
    }

    /**
     * 创建并返回M3U文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/M3U/local.m3u
     */
    fun geM3UFilePath(fileDirPath: String?): String? {
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) return null
        // 创建文件夹
        val targetFileDirPath = fileDirPath + File.separator + LOCAL_M3U_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath + File.separator + LOCAL_M3U_FILE_NAME
    }

    /**
     * 删除创建的代理文件
     */
    fun deleteM3UFile(fileDirPath: String?){
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) return
        // 创建文件夹
        val targetFilePath = fileDirPath + File.separator + LOCAL_M3U_FILE_DIR_NAME + File.separator + LOCAL_M3U_FILE_NAME
        FileUtils.delete(targetFilePath)
    }

    /**
     * 创建并返回Keys文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/Keys/
     */
    fun getM3U8KeyFileDirPath(fileDirPath: String?): String?{
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) {
            return null
        }
        // 创建文件夹
        val targetFileDirPath = fileDirPath + File.separator +KEY_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath
    }

    fun deleteM3U8KeyFile(fileDirPath: String?){
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) {
            return
        }
        // 创建文件夹
        val targetFilePath = fileDirPath + File.separator + KEY_FILE_DIR_NAME
        FileUtils.deleteAllInDir(targetFilePath)
    }

    /**
     * 创建并返回Proxy文件的M3U文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/Proxy/proxy.m3u8
     */
    fun getOrCreateProxyM3U8File(fileDirPath: String?): String? {
        if (TextUtils.isEmpty(fileDirPath)) return null
        // 创建文件夹
        val targetFileDirPath = fileDirPath + File.separator + PROXY_M3U_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        val path = targetFileDirPath + File.separator + PROXY_M3U_FILE_NAME
        val isCreateFile = FileUtils.createOrExistsFile(path)
        if (!isCreateFile) {
            return null
        }
        return path
    }

    /**
     * 删除全部下载文件
     */
    fun deleteDownloadFile(fileDirPath: String?){
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) {
            return
        }
        val file = File(fileDirPath)
        if (!file.exists()) return
        file.deleteRecursively()
    }

    fun deleteProxyM3U8File(fileDirPath: String?){
        if (fileDirPath == null || TextUtils.isEmpty(fileDirPath)) {
            return
        }
        // 创建文件夹
        val targetFilePath = fileDirPath + File.separator + PROXY_M3U_FILE_DIR_NAME
        FileUtils.deleteAllInDir(targetFilePath)
    }

    /**
     * 创建并返回Proxy文件的M3U文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/TsList/
     */
    fun getOrCreateTsFileDirPath(fileDirPath: String?): String? {
        if (TextUtils.isEmpty(fileDirPath)) return null
        // 创建文件夹
        val targetFileDirPath = fileDirPath + File.separator + TS_LIST_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath
    }

    /**
     * 创建并返回ts的文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/TsList/001.ts
     */
    fun getTsFilePath(fileDirPath: String?, tsFilePath: String): String{
        return fileDirPath + File.separator + tsFilePath
    }



    /**
     * 创建并返回mp4的文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/Mp4/final.mp4
     */
    fun getMp4FilePath(fileDirPath: String?): String? {
        if (TextUtils.isEmpty(fileDirPath)) return null
        // 创建文件夹
        val targetFileDirPath = fileDirPath + File.separator + MP4_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath + File.separator + MP4_FILE_NAME
    }


    /**
     * 根据视频名称，创建当前视频下载的根目录
     * @param fileName
     *   /Data/Android/xxx/files/Wanban/${fileName}
     */
    fun getCacheTsListFilePath(fileName: String): File? {
        val root = getOrCreateDownloadRootFileDirPath()?:return null
        val videoFileDirPath = root + File.separator + CACHE_TS_LIST_FILE_DIR_NAME + File.separator + CACHE_TS_LIST_FILE_NAME
        val isExitFile = FileUtils.isFileExists(videoFileDirPath)
        return if (isExitFile) {
            File(videoFileDirPath)
        } else null
    }

    fun getCoverFilePath(fileDirPath: String?): String? {
        if (TextUtils.isEmpty(fileDirPath)) return null
        return fileDirPath + File.separator + COVER_FILE_NAME
    }


    /**
     * 创建并返回m3u8的文件路径
     * @param fileName
     * /Data/Android/xxx/files/Wanban/${fileName}/Cache/cacheM3U8.txt
     */
    fun getLocalCacheM3U8FilePath(fileName: String?): String? {
        if (TextUtils.isEmpty(fileName)) return null
        val rootPath = getOrCreateVideoRootFileDirPath(fileName!!)
        if (TextUtils.isEmpty(rootPath)) return null
        // 创建文件夹
        val targetFileDirPath = rootPath + File.separator + LOCAL_CACHE_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath + File.separator + LOCAL_CACHE_M3U8_FILE_NAME
    }


    /**
     * /Data/Android/xxx/files/Wanban/${fileName}/Cache/LocalTsList
     */
    fun getLocalTsListFileDirPath(fileName: String?): String? {
        if (TextUtils.isEmpty(fileName)) return null
        val rootPath = getOrCreateVideoRootFileDirPath(fileName!!)
        if (TextUtils.isEmpty(rootPath)) return null
        // 创建文件夹
        val targetFileDirPath = rootPath + File.separator + LOCAL_CACHE_FILE_DIR_NAME
        val isCreateDir = FileUtils.createOrExistsDir(targetFileDirPath)
        if (!isCreateDir) {
            return null
        }
        return targetFileDirPath + File.separator + LOCAL_M3U8_FILE_DIR_NAME
    }
}